package app.kotlin_1.activity

import android.support.v4.view.PagerAdapter
import android.view.View
import android.view.ViewGroup
import app.kotlin_1.R
import app.kotlin_1.adapter.GameKindViewPagerAdapter
import app.kotlin_1.base.BaseActivity
import app.kotlin_1.bean.GameBean
import com.facebook.drawee.view.SimpleDraweeView
import kotlinx.android.synthetic.main.activity_game_kind.*
import kotlinx.android.synthetic.main.title_back.*
import android.R.attr.paddingLeft
import android.content.Context
import android.view.MotionEvent
import app.kotlin_1.utils.*
import android.support.v4.view.ViewPager
import android.widget.RelativeLayout
import android.opengl.ETC1.getHeight
import android.opengl.ETC1.getWidth
import android.content.Context.WINDOW_SERVICE
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.WindowManager
import android.widget.LinearLayout
import app.kotlin_1.adapter.GalleryAdapter
import android.view.ViewGroup.MarginLayoutParams
import app.kotlin_1.adapter.GameKindSheJiAdapter
import app.kotlin_1.base.BaseRecyclerViewAdapter
import org.jetbrains.anko.toast


/**
 * Created by 刘智强 on 2017/9/20.
 * 游戏分类的详情页
 *
 */
class GameKindActivit : BaseActivity() {
    private val viewBannerList = mutableListOf<View>()
    private val imageUrlList = mutableListOf<String>()
    private val hotGameList = mutableListOf<GameBean>()
    private val hotGameViewList = mutableListOf<View>()
    private val galleryGameList = mutableListOf<GameBean>()
    private val gameSheJiList = mutableListOf<GameBean>()
    override fun getlayoutId(): Int {
        return R.layout.activity_game_kind

    }

    override fun getData() {
        var bundle = intent.extras
        var title:String?=bundle?.getString("title")
        tvTitle.text = title
    }

    override fun initData() {
        ivDownload.visibility = View.GONE
        initBannerViewPager()
        initHotGameViewPager()
        initGallery()
        initSheJi()
    }

    override fun initView() {
    }

    override fun initListenter() {
        rlMore.setOnClickListener(this)
        ivBack.setOnClickListener(this)
        rlTaFang.setOnClickListener(this)

    }


    override fun requestData() {
    }

    override fun setData(any: Any) {
    }

    override fun setOnClick(v: View?) {

        when (v!!.getId()) {
            R.id.ivBack -> finish()
            R.id.rlMore -> toast("更多")
            R.id.rlTaFang -> toast("塔防")
        }

    }

    private fun initBannerViewPager() {
        imageUrlList.add("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2461112560,680770050&fm=27&gp=0.jpg")
        imageUrlList.add("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2262508533,4178760192&fm=27&gp=0.jpg")
        imageUrlList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505910884843&di=21e776ec970d5104fe916ef0cb5e90ec&imgtype=0&src=http%3A%2F%2Fimg.sy.kuaiyong.com%2Fnews%2Fgl%2F2015-03-17%2F114ed10f119de2752137c8c1a200a0d9.jpg")
        imageUrlList.forEach {
            var spd = SimpleDraweeView(activity)
            spd.setImageURI(it)
            viewBannerList.add(spd)
        }
        vpGameKindBanner.adapter = object : PagerAdapter() {
            override fun isViewFromObject(view: View?, `object`: Any?): Boolean {
                return view == `object`
            }

            override fun getCount(): Int = imageUrlList.size


            override fun instantiateItem(container: ViewGroup?, position: Int): Any {
                container!!.addView(viewBannerList[position])
                return viewBannerList[position]
            }

            override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
                container!!.removeView(viewBannerList[position])
            }

        }
    }

    private fun initHotGameViewPager() {

        hotGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505897667918&di=b9bbab96913ba0fa1d50cf02b11fa2da&imgtype=0&src=http%3A%2F%2Fimg2.gtimg.com%2Fdbimg%2F2013%2Fmainpiclib%2F20131126%2F16%2F7dc28658e59b9a3d3de63360e1a1dcb22.jpg", "炉石传说"))
        hotGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505897726144&di=f94334daf1b49e104d839fb7fedb5b89&imgtype=0&src=http%3A%2F%2Fimg1.gamedog.cn%2F2013%2F08%2F27%2F95-130RFU9110.jpg", "轩辕传奇"))
        hotGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506492497&di=5f4a7ccb3c2b8d255df0274674426144&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.3dmgame.com%2Fuploads%2Fallimg%2F161215%2F346_161215174407_1.jpg", "阴阳师"))
        hotGameList.add(GameBean("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2099374589,3947003001&fm=11&gp=0.jpg", "梦幻西游"))
        hotGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506492604&di=dab1c6279dd0260707180b60c71fbe63&imgtype=jpg&er=1&src=http%3A%2F%2Fnewsimg.5054399.com%2Fuploads%2Fuserup%2F1610%2F1111364W3D.jpg", "王者荣耀"))
        hotGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506492657&di=bf2770ceedb9de46bc1bfbd856db60b7&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.xskhome.com%2Fpic%2F2017%2F08%2F20170805%2Fclooect%2F173651_1623889403.jpg", "我的世界"))

        hotGameList.forEach {
            var spd = SimpleDraweeView(activity)
            spd.setImageURI(it.icon)
            hotGameViewList.add(spd)
        }
        var adapter = GameKindViewPagerAdapter(activity, hotGameList)
        val wm = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val width = wm.defaultDisplay.width
        val height = wm.defaultDisplay.height
        val params = LinearLayout.LayoutParams(width * 5 / 10, height * 6 / 20)
        vpHotGame.layoutParams = params
        vpHotGame.offscreenPageLimit = 3
        vpHotGame.pageMargin = 8
        vpHotGame.setPageTransformer(true, GalleyTransFormer())//要在setadapter之前
        llViewPager.setOnTouchListener { _, event -> vpHotGame.dispatchTouchEvent(event) }//解决点击缝隙不能滑动的问题 当点击缝隙的时候 父布局不拦截，传递给viewpager
        vpHotGame.adapter = adapter
    }

    private fun initGallery() {
        galleryGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505995323225&di=103badf27b8ae29cf925689a8f6f0eec&imgtype=0&src=http%3A%2F%2Fpic100.nipic.com%2Ffile%2F20160605%2F23324175_111518288000_2.jpg", "植物大战僵尸"))
        galleryGameList.add(GameBean("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1027189787,3617982638&fm=27&gp=0.jpg", "部落冲突"))
        galleryGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505995405101&di=db1cd91aac9d1f42324791aba259657e&imgtype=0&src=http%3A%2F%2Fa1.att.hudong.com%2F17%2F87%2F20300543887706146664874036763.jpg", "皇室战争"))
        galleryGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505995437926&di=5d2364d8619db672df5c9f7e20c03306&imgtype=0&src=http%3A%2F%2Fpic3.16pic.com%2F00%2F17%2F90%2F16pic_1790648_b.jpg", "保卫萝卜"))
        galleryGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505995527208&di=0c2a157e1283527e9ffc1c69d9259083&imgtype=0&src=http%3A%2F%2Fupload.gezila.com%2Fdata%2F20160727%2F73881469591839.jpg", "海岛奇兵"))
        galleryGameList.add(GameBean("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=523701360,3025546122&fm=27&gp=0.jpg", "激战僵尸"))

        galleryTaFang.adapter = GalleryAdapter(activity, galleryGameList)
        val layoutParams = galleryTaFang.layoutParams as MarginLayoutParams
        val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val width = wm.defaultDisplay.width
        layoutParams.setMargins(-width * 2 / 3, 0, 0, 0)
        galleryTaFang.setOnItemClickListener { parent, view, position, id ->
            run {
                val intent = Intent(activity, GameDetailActivity::class.java)
                val bundle = Bundle()
                bundle!!.putSerializable("game", galleryGameList[position])
                intent.putExtras(bundle)
                startActivity(intent)
            }
        }

    }

    private fun initSheJi() {
        var layoutManager = LinearLayoutManager(activity)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        rvSheJi.layoutManager = layoutManager
        var adapter = GameKindSheJiAdapter(activity, galleryGameList, R.layout.item_gallery)
        adapter.onItemClickListener = object : BaseRecyclerViewAdapter.OnItemClickListener {
            override fun onClick(v: View, position: Int) {
                val intent = Intent(activity, GameDetailActivity::class.java)
                val bundle = Bundle()
                bundle!!.putSerializable("game", galleryGameList[position])
                intent.putExtras(bundle)
                startActivity(intent)
            }

            override fun onLongClick(v: View, Position: Int) {
            }

        }
        rvSheJi.adapter = adapter
    }


}
